<%@ page import="org.springframework.web.context.request.SessionScope" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%request.setAttribute("ctx", request.getContextPath());%>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="/layui-v2.9.14/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/jquery-2.1.0.min.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/layui_ext/xmSelect/xm-select.js"></script>
    <script src="${pageContext.request.contextPath}/layui-v2.9.14/crypto-js.js"></script>
    <title></title>
</head>
<body>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-block">
            <input type="hidden" name="id" id="roleId">
            <input type="text" name="roleName" lay-verify="required" id="roleName"  placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div id="ID-tree-demo-showCheckbox"></div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script>
    layui.use(function(){
        var form = layui.form;
        var tree = layui.tree;

        // 模拟数据
        var initData=JSON.parse('${treeMenuList}');

        //监听rolename 输入框
        $("#roleName").on('blur',function () {
            //获取输入框的值
            let roleName=$("#roleName").val();
            //检查角色是否存在 -如果角色存在需要回显该角色拥有菜单权限
          $.get("/role/checkRoleName.do",{'roleName':roleName},function (res){
              //角色存在
             if(res.code ==0){
                 layer.msg("角色已存在",{time:1000},function (){
                     //为隐藏域赋值
                     $("#roleId").val(res.data.roleId)
                     // 设置对应 树形 id 的节点选中
                     tree.setChecked('test', res.data.menuIds); // 批量勾选 id 为 1,3 的节点
                 })

             }else{
                 layer.msg("角色不存在!",{time:2000},function (){
                     console.log(initData)
                     // 重载
                     tree.reload('test', { // options
                         data: JSON.parse('${treeMenuList}')
                     });
                 })



             }
          })
        })



         // 渲染
        tree.render({
            elem: '#ID-tree-demo-showCheckbox',
            data: initData,
            showCheckbox: true,
            edit: ['add', 'update', 'del'] ,// 开启节点的右侧操作图标
            id: 'test', // 自定义 id 索引
            customName: { // 自定义 data 字段名 --- 2.8.14+
                id: 'menuId',
                title: 'name',
                children: 'children'
            },
        });

        // 搜索提交
        form.on('submit(demo1)', function(data){
            var field = data.field; // 获得表单字段
            // 获取选中的节点数据
            var checkData = tree.getChecked('test');
            console.log(checkData)
            let ids="";
            for (let i = 0; i < checkData.length; i++) {
               ids+=","+checkData[i].menuId;
                let children= checkData[i].children;
                for (let j = 0; j <children.length; j++) {
                    ids+=","+children[j].menuId;
                }
            }
          //  console.log(ids.substring(1))
            field.menuIds=ids.substring(1);
            console.log(field)
            $.post("/role/add.do",field, function (res){
                if(res.code==0){
                    layer.msg(res.msg,{time:2000},function (){
                        location.href="/role/tolist.do"
                    })
                }else{
                    layer.msg(res.msg,{time:2000})
                }
            })
            return false; // 阻止默认 form 跳转
        });
    });
</script>

</body>
</html>